//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    //测试视频列表
    videoList: [
      
     ],
    playerType:'video',
    fitType:'contain',
    cat_list: [],
    cat_id: 0,
    index: 0
  },
  //修改视频属性 保证只有一个video被创建
  controlVideoPlayer: function (list, index) {
    if(list.length===0){
       return [];
    }else{
       list.forEach((item,i)=>{
         if (index === i){
          item.video_is_player=true;
         }else{
           item.video_is_player=false;
         }
       });
       return list;
    }
  },
  onLoad:function(options){
    getApp().page.onLoad(this, options);
    var that = this;
    if (options.cat_id) {
      this.setData({
        cat_id: options.cat_id
      })
    }
    getApp().request({
      url: getApp().api.vgoods.edit,
      data:{id: options.id,cat_id:options.cat_id},
      success: function (t) {
        if (0 == t.code) {
          t.data.vgoods = that.xuanran_good(t.data.vgoods);
          let videolist = that.controlVideoPlayer(t.data.vgoods, 0);
          that.setData({
            videoList: videolist
          });
        }else {
          wx.showToast({
            icon: 'none',
            title: t.msg
          })
          setTimeout(function() {
            wx.navigateBack(-1)
          },1500)
        }
      }
    });
    this.get_cat_list();
  },
  get_cat_list: function () {
    var that = this;
    getApp().request({
      url: getApp().api.vgoods.get_cat,
      success: function (t) {
        if (0 == t.code) {
          that.setData({
            cat_list: t.data.cart_list
          });
        }
      }
    });
  },
  onShow:function () {
    getApp().page.onShow(this);
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
          that.setData({
            statusBarHeight : res.statusBarHeight
          })
       }
    })
    wx.hideShareMenu()
  },
  //上滑事件
  swipeUpper:function(e){
    var that = this;
    var index = e.detail.index;
    that.setData({
      index:index
    })
    var videoSize = this.data.videoList.length;
    if (index+4 >= videoSize) {
      setTimeout(function() {
        that.swipeToEnd();
      },1000);
    }
    let videolist = this.controlVideoPlayer(this.data.videoList, index);
    setTimeout(function () {
      that.setData({
        videoList: videolist
      });
    },300)
  },
  swipeToEnd: function (e=false) {
    var that = this;
    var videoList = this.data.videoList;
    var vgoods_id = !e ? videoList[videoList.length-1].id - 1 : 0;
    getApp().request({
      url: getApp().api.vgoods.edit,
      data:{id: vgoods_id,cat_id:that.data.cat_id},
      success: function (t) {
        if (0 == t.code) {
          t.data.vgoods = that.xuanran_good(t.data.vgoods);
          var videolistData = that.data.videoList.concat(t.data.vgoods)
          that.setData({
            videoList: videolistData
          });
        }
      },
      complete: function(res) {
        wx.hideLoading()
      }
    });
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    let vgoods = res.target.dataset.shareinfo;
    return {
      path: "/vgoods/edit/index?id=" + vgoods.id + '&store_id=' + (wx.getStorageSync("store_id") ? wx.getStorageSync("store_id") : 0) ,
      imageUrl: vgoods.cover_pic,
      title: vgoods.title,
      success: function (t) {
        
      },
    };
  },
  onShareTimeline: function () {
    var vgoods = this.data.videoList[this.data.index];
    return {
      query:{
        id: vgoods.id,
        store_id: wx.getStorageSync("store_id") ? wx.getStorageSync("store_id") : 0
      },
      title: vgoods.title,
      imageUrl: vgoods.cover_pic
    };
  },
  xuanran_good: function(tt) {console.log(tt);
    var that = this;
    var good_vgoods = wx.getStorageSync('good_vgoods');
    console.log(good_vgoods);
    if (good_vgoods) {
      for (var i = 0; i < tt.length; i++) {
        if (good_vgoods.indexOf(tt[i].id) > -1) {
          tt[i].good = 1
        } else {
          tt[i].good = 0
        }
      }
      return tt;
    }else {
      return tt;
    }
  },
  show_user: function (params) {
    this.setData({
      user_info_show: !0
    })
  },
  updateCat:function (e) {
    var cat_id = e.currentTarget.dataset.cat_id;
    wx.showLoading({
      title: "加载中"
    });
    wx.redirectTo({
      url: '/vgoods/edit/index?cat_id=' + cat_id
    })
  },
  back: function () {

    //获取页面栈
    var pages = getCurrentPages();
    if(pages.length > 1){ //说明有上一页存在
      wx.navigateBack(-1)
    }else {
      wx.switchTab({
        url: '/pages/index/index',
      })
    }
  },
  hideModal: function() {
      this.setData({
          showModal: !1
      });
  },
})
